<?php
/**
 * Image.class.php
 * 图片处理工具类
 *
 * @author 王中艺 
 * @date   2016-12-20
 */
namespace Wechat\Util;

class Image {

    /**
     * 我的相册图片生成
     * @param   $themeId
     * @param   $picture
     * @return  image
     */
    function beautifyPicture($themeId, $picture){
        // header('content-type: image/jpeg');
        $image  = imagecreatetruecolor(752, 1206);

        $themeInfo  = C('beautify_theme.'.$themeId);
        //照片插入
        $picSize    = getimagesize($picture);
        $sizeX  = $picSize[0];
        $sizeY  = $picSize[1];

        if(($sizeX - $themeInfo['show_w']) < ($sizeY - $themeInfo['show_h'])){
            //以X轴为准缩放
            $resizeX    = $themeInfo['show_w'];
            $resizeY    = $sizeY * ($themeInfo['show_w']/$sizeX);
        }else{
            //以Y轴为准缩放
            $resizeY    = $themeInfo['show_h'];
            $resizeX    = $sizeX * ($themeInfo['show_h']/$sizeY);
        }
        $posX   = $themeInfo['show_x'] - ($resizeX - $themeInfo['show_w']) * 0.5;
        $posY   = $themeInfo['show_y'] - ($resizeY - $themeInfo['show_h']) * 0.5;

        $picture   = imagecreatefromjpeg($picture);
        imagecopyresampled($image, $picture, $posX, $posY, 0, 0, $resizeX, $resizeY, $sizeX, $sizeY);

        //美化框架插入
        $frame   = imagecreatefrompng($themeInfo['img_url']);
        imagecopyresampled($image, $frame, 0, 0, 0, 0, 752, 1206, 752, 1206);

        //保存图片
        $fileName   = 'Uploads/PhotoAlbum/'.date('YmdHis-').rand(10000, 99999).'.jpg';
        imagejpeg($image, $fileName);
        imagedestroy($image);

        return '/server/'.$fileName;
    }

    /**
     * 生成‘我的二维码’图片
     * @param   $userInfo 用户信息
     * @return  png图片
     */
    function createQrCode($userInfo){
        header('content-type: image/jpeg');
        $image  = imagecreatefromjpeg('./Public/Image/bg-myqrcode.jpg');

        //头像添加
        // $headIcon    = gd_radius_img('./Public/Image/logo.png', 50);

        //姓名添加
        $gray  = imagecolorallocate($image, 90, 90, 90);
        $name   = $userInfo['nickname'];
        $font   = './Public/font.ttf';
        $nameBox    = imagettfbbox(30, 0, $font, $name);
        imagettftext($image, 30, 0, (600-$nameBox[2])*0.5+40, 550, $gray, $font, $name);

        //二维码图片添加
        $qrCode     = D('User')->getQRCode($userInfo['id']);
        $qrCodeImg  = create_2d_code($qrCode['norm_link'], true);
        imagecopyresampled($image, $qrCodeImg[0], 210, 850, 0, 0, 260, 260, $qrCodeImg[1][0], $qrCodeImg[1][1]);

        imagejpeg($image);
        imagedestroy($image);
    }

    /**
     * 生成课程邀请码图片
     * @param   $userId 用户ID
     * @param   $courseId 课程ID
     * @return   PNG图片
     */
    function createCourseShare($userId, $courseId, $themeId = 1){
        header('content-type: image/jpeg');

        $userInfo   = D('User')->getUserInfo($userId);
        $courseInfo = D('Course')->detail($courseId, $userInfo['id']);
        $themeInfo  = C('SHARE_COURSE_THEME.'.$themeId);

        $image  = imagecreatefromjpeg($themeInfo['bg_image']);
        $black  = imagecolorallocate($image, 30, 30, 30);
        $green  = imagecolorallocate($image, 64, 171, 61);
        $orange     = imagecolorallocate($image, 241, 159, 19);
        $gray   = imagecolorallocate($image, 60, 60, 60);
        $font   = './Public/font.ttf';

        //课程主标题添加
        $mainTitleY = $themeInfo['main_title_y'];
        $mainTitle  = $courseInfo['main_title'];
        $mTitleBox  = imagettfbbox(25, 0, $font, $mainTitle);
        imagettftext($image, 25, 0, (600-$mTitleBox[2])*0.5, $mainTitleY, $green, $font, $mainTitle);
        imagettftext($image, 25, 0, (600-$mTitleBox[2])*0.5+1, $mainTitleY+1, $green, $font, $mainTitle);
        //课程副标题添加
        $subTitleY  = $themeInfo['sub_title_y'];
        $subTitle1  = msubstr($courseInfo['sub_title'], 0, 18, 'utf-8', false);
        $sTitleBox  = imagettfbbox(18, 0, $font, $subTitle1);
        imagettftext($image, 18, 0, (600-$sTitleBox[2])*0.5, $subTitleY, $gray, $font, $subTitle1);
        $subTitle2  = msubstr($courseInfo['sub_title'], 18, 20, 'utf-8', false);
        $sTitleBox  = imagettfbbox(18, 0, $font, $subTitle2);
        imagettftext($image, 18, 0, (600-$sTitleBox[2])*0.5, $subTitleY+40, $gray, $font, $subTitle2);
        //用户名称添加
        $userNameX  = $themeInfo['username_x'];
        $userNameY  = $themeInfo['username_y'];
        $userName   = $userInfo['nickname'];
        imagettftext($image, 18, 0, $userNameX, $userNameY, $green, $font, $userName);
        imagettftext($image, 18, 0, $userNameX+1, $userNameY+1, $green, $font, $userName);
        //二维码添加
        $qrCodeX    = $themeInfo['qrcode_x'];
        $qrCodeY    = $themeInfo['qrcode_y'];
        $qrCode     = D('User')->getQRCode($userInfo['id']);
        $qrCodeImg  = create_2d_code($qrCode['norm_link'], true);
        imagecopyresampled($image, $qrCodeImg[0], $qrCodeX, $qrCodeY, 0, 0, 150, 150, $qrCodeImg[1][0], $qrCodeImg[1][1]);

        imagejpeg($image);
        imagedestroy($image);
    }

    /**
     * 生成课程邀请码图片
     * @param   $userId 用户ID
     * @param   $courseId 课程ID
     */
    function createCourseGraduate($userId, $courseId, $themeId = 1){
        header('content-type: image/jpeg');
        $userInfo   = D('User')->getUserInfo($userId);
        $courseInfo = D('Course')->detail($courseId, $userId);
        $themeInfo  = C('SHARE_GRADEUATE_THEME.'.$themeId);

        $image  = imagecreatefromjpeg($themeInfo['bg_image']);
        $font   = './Public/font.ttf';
        $black  = imagecolorallocate($image, 40, 30, 30);

        // //文字描述添加
        // $fontX  = $themeInfo['font_x'];
        // $fontY  = $themeInfo['font_y'];
        // $fontSize   = 16;
        // $lineHeight = 40;
        // imagettftext($image, $fontSize, 0, $fontX, $fontY, $black, $font, '亲爱的家长:');
        // $fontY  = $fontY + $lineHeight;
        // $allText    = '　　恭喜您完成《'.$courseInfo['name'].'》课程，特发此证书';
        // $start  = 0;
        // $length     = 20;
        // $text   = msubstr($allText, $start, $length, 'utf-8', false);
        // while($text){
        //     imagettftext($image, $fontSize, 0, $fontX, $fontY, $black, $font, $text);
        //     $fontY  += $lineHeight;
        //     $start  += $length;
        //     $text   = msubstr($allText, $start, $length, 'utf-8', false);
        // }
        // //日期添加
        // $dateX  = $themeInfo['date_x'];
        // $dateY  = $themeInfo['date_y'];
        // $dateText   = date('Y-m-d');
        // imagettftext($image, $fontSize, 0, $dateX, $dateY, $black, $font, $dateText);
        $courseName     = '《'.$courseInfo['name'].'》';
        $courseNameBox  = imagettfbbox(16, 0, $font, $courseName);
        imagettftext($image, 16, 0, (600-$courseNameBox[2])*0.5, $themeInfo['course_name_y'], $black, $font, $courseName);

        //二维码添加
        $qrCodeX    = $themeInfo['qrcode_x'];
        $qrCodeY    = $themeInfo['qrcode_y'];
        $qrCode     = D('User')->getQRCode($userInfo['id']);
        $qrCodeImg  = create_2d_code($qrCode['norm_link'], true);
        imagecopyresampled($image, $qrCodeImg[0], $qrCodeX, $qrCodeY, 0, 0, 240, 240, $qrCodeImg[1][0], $qrCodeImg[1][1]);

        imagejpeg($image);
        imagedestroy($image);
    }

    /**
     * 生成幸福语录图片
     * @param   $userId
     * @param   $mottoId
     * @return  image
     */
    function createMottoImg($userId, $mottoId){
        $userInfo   = D('User')->getUserInfo($userId);
        $mottoInfo  = D('Motto')->getItem($mottoId);
        if($mottoInfo === false){
            echo D('Motto')->errMsg;
            exit;
        }

        //图片：600*600 jpg
        $image  = strchr($mottoInfo['picture'], 'Uploads');
        if(!file_exists($image)){
            echo '文件缺失!';
            exit;
        }
        header('Content-type: image/jpeg');
        $image  = imagecreatefromjpeg($image);
        $font   = './Public/font.ttf';
        $black  = imagecolorallocate($image, 60, 60, 60);
        $name   = $userInfo['nickname'];

        imagettftext($image, 18, 0, 65, 195, $black, $font, $name);


        //二维码图片添加
        $qrCode     = D('User')->getQRCode($userInfo['id']);
        $qrCodeImg  = create_2d_code($qrCode['norm_link'], true);
        imagecopyresampled($image, $qrCodeImg[0], 440, 460, 0, 0, 120, 120, $qrCodeImg[1][0], $qrCodeImg[1][1]);                                     

        imagejpeg($image);
        imagedestroy($image);
    }
}
